class @CustomView extends Backbone.View
    initialize:->
        $(@el).off()
        Loading.Start()
        _.bindAll(@)
        @model.view = @

    Navigate: (url , trigger, replace) ->
        Backbone.history.navigate(url.replace(window.UrlPrefix,''), 
        { 
            trigger: trigger 
            replace: replace
        })

    GetTemplate: (url, func) ->
        Utilites.GetTemplate(url, func, @initializeValidate)

    initializeValidate: ->
        form = $(@el).find("form")
        form.find('input, textarea, select').each(()->
           $(@).Validator()
        )

    IsValid: ->
        if $(@el).find("fieldset").find("div[data-error-message]").length
            return false
        return true

class Utilites
    @template: []

    @GetTemplate: (url, func, customFunc) ->
        resolvedUrl = url
        if Settings.Compress.IsEnabled
            resolvedUrl = url.replace("/myLibrary/", '/myLibrary/Content/') + '.html'
        cashedTemplate = Utilites.template[url + Settings.Version]

        if typeof (cashedTemplate) == 'undefined'
            $.ajax(
                url: resolvedUrl
                cache: true
                success: (template) ->
                    compiledTemplate = _.template(template)
                    Utilites.template[url + Settings.Version] = compiledTemplate
                    func(compiledTemplate)
                    customFunc()
                    Loading.Stop()
                error: ->
                    Messages.WriteErrors([{Message:"Could not find template for url #{url}"}])
                    Messages.WriteMessage("Trying get template with safe url")
                    $.ajax(
                       url: url
                       cache: true
                       success: (template) ->
                           compiledTemplate = _.template(template)
                           Utilites.template[url + Settings.Version] = compiledTemplate
                           func(compiledTemplate)
                           customFunc()
                           Loading.Stop()
                    )
            )
        else
            func(cashedTemplate)
            customFunc()
            Loading.Stop()
